package com.example.qqfragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageFragment extends Fragment {
    private Context context;
    private ListView messagelistView;

    List<Map<String,Object>> dataList=new ArrayList<Map<String, Object>>();
    String [] Names={"张三","李四","王五","贾七"};
    int [] Images={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d};

    //创建SimpleAdapter对象
    private SimpleAdapter simpleAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_message,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context=getActivity();
        initView();
    }

    private void initView() {
        messagelistView=getActivity().findViewById(R.id.messagelistView);
        getData();
        simpleAdapter=new SimpleAdapter(context,dataList,R.layout.item_listview,
                new String[]{"Name","Image"},
                new int[]{R.id.textView,R.id.imageView});
        messagelistView.setAdapter(simpleAdapter);
        messagelistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取当前项的数据
//                String name=dataList.get(position).get("Name").toString();
//                Intent intent=new Intent(getActivity(),TalkActivity.class);
//                startActivity(new Intent(getActivity(),TalkActivity.class));
//                intent.putExtra("name",name);
//                Toast.makeText(context, ""+name, Toast.LENGTH_SHORT).show();
                String name=dataList.get(position).get("Name").toString();
                Intent intent=new Intent(getActivity(),TalkActivity.class);
                intent.putExtra("name",name);
                startActivity(intent);


            }
        });
    }
    //list获取数据
    private void getData() {
        for(int i=0;i<Names.length;i++){
            HashMap<String,Object> map=new HashMap<String,Object>();
            map.put("Name",Names[i]);
            map.put("Image",Images[i]);
            dataList.add(map);
        }
    }

}
